package link.xieyu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import link.xieyu.pojo.AppArticle;
import link.xieyu.pojo.BadRequestException;
import link.xieyu.pojo.bo.AppContextBO;
import link.xieyu.service.ContextService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author xieyu
 */
@Api(tags = "文章相关接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("app/context")
public class ContextController extends BaseController {
    private final ContextService contextService;
    /**
     * 文章最少字数
     */
    @Value("${app.context.minSize:30}")
    private int minSize;

    @ApiOperation("发表文章,图片直接base64位发过来就好")
    @GetMapping
    public ResponseEntity<Object> create(@RequestBody AppContextBO appContextBO) {
        if (StringUtils.hasLength(appContextBO.getContext()) && appContextBO.getContext().length() < minSize) {
            throw new BadRequestException("正文字数最少30!");
        }
        appContextBO.setId(getUser().getId());
        contextService.create(appContextBO);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("修改文章,将原有信息携带过来")
    @PutMapping
    public ResponseEntity<Object> update(@RequestBody AppContextBO appContextBO) {
        if (StringUtils.hasLength(appContextBO.getContext()) && appContextBO.getContext().length() < minSize) {
            throw new BadRequestException("正文字数最少30!");
        }
        appContextBO.setId(getUser().getId());
        contextService.update(appContextBO);
        return ResponseEntity.ok().build();
    }

}
